//
//  UseMusicPlayer.swift
//  learn-ios
//
//  Created by Dong on 2022/4/5.
//

import SwiftUI

import AVFoundation







struct UseMusicPlayer: View {
    @State private var audioPlayer: AVAudioPlayer?
    @State private var isPlayer = false
    
    let path = Bundle.main.path(forResource: "bababa", ofType: "mp4")!
    
    
    
    var body: some View {
        VStack {
            Button {
                let url = URL(fileURLWithPath: path)
                do {
                    audioPlayer = try AVAudioPlayer(contentsOf: url)
                    if !isPlayer {
                        audioPlayer?.play()
                    }else{
                        audioPlayer?.stop()
                    }
                    isPlayer.toggle()
                } catch  {
                    print("播放失败\(error)")
                }
            } label: {
                Text(isPlayer ? "暂停播放":"开始播放")
                    .font(.title)
            }
        }
    }
}

struct UseMusicPlayer_Previews: PreviewProvider {
    static var previews: some View {
        UseMusicPlayer()
    }
}
